package com.baizhi.shane.sys.controller;

import com.baizhi.shane.common.base.Result;
import com.baizhi.shane.constant.ApiMapping;
import com.baizhi.shane.sys.entity.SysUser;
import com.baizhi.shane.sys.service.SysUserService;
import com.baizhi.shane.sys.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("SysUser")
@Api(tags="用户")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 注册
     *
     * @Author 闻柏智
     * @Date 21:56 2023/3/21
     * @Param userSignUpVO
     * @return boolean
     **/
    @ApiOperation(value="注册", notes="注册")
    @PostMapping(value = "/signUp")
    public Result<UserSignUpOutVO> signUp(
            @RequestBody UserSignUpInVO userSignUpInVO
            ) {
        UserSignUpOutVO userSignUpOutVO = sysUserService.signUp(userSignUpInVO);
        return Result.OK(userSignUpOutVO);
    }

    /**
     * 注册
     *
     * @Author 闻柏智
     * @Date 21:56 2023/3/21
     * @Param userLoginInVO
     * @return UserLoginOutVO
     **/
    @ApiOperation(value="登录", notes="登录")
    @PostMapping(value = "/login")
    public Result<UserLoginOutVO> login(
            @RequestBody UserLoginInVO userLoginInVO
    ) {
        UserLoginOutVO userLoginOutVO = sysUserService.login(userLoginInVO);
        return Result.OK(userLoginOutVO);
    }

    /**
     * 获取当前登陆人信息
     *
     * @Author 吴若琦
     * @Date 16:22 2023/7/29
     * @Param userLoginInVO
     * @return com.baizhi.shane.sys.vo.UserLoginOutVO
     **/
    @ApiOperation(value="获取登录用户信息", notes="获取登录用户信息")
    @GetMapping(value = ApiMapping.GET_LOGIN_USER_INFO)
    public Result<LoginUserVO> getLoginUserInfo() {
        LoginUserVO loginUserVO = sysUserService.getLoginUserInfo();
        return Result.OK(loginUserVO);
    }

    /**
     * 用户退出登录
     *
     * @Author 闻柏智
     * @Date 11:08 2023/10/25
     * @Param
     * @return com.baizhi.shane.common.base.Result<java.lang.String>
     **/
    @ApiOperation(value="用户退出登录", notes="用户退出登录")
    @PostMapping(value = ApiMapping.LOGIN_OUT)
    public Result<String> loginOut() {
        String success = sysUserService.loginOut();
        return Result.OK(success);
    }

    /**
     * 用户修改用户名
     *
     * @Author 闻柏智
     * @Date 23:56 2023/10/29
     * @Param updateUsernameVO
     * @return com.baizhi.shane.common.base.Result<java.lang.String>
     **/
    @ApiOperation(value="用户修改用户名", notes="用户修改用户名")
    @PostMapping(value = ApiMapping.UPDATE_USERNAME)
    public Result<String> updateUsername(UpdateUsernameVO updateUsernameVO) {
        String success = sysUserService.updateUsername(updateUsernameVO);
        return Result.OK(success);
    }

    /**
     * 用户修改密码
     *
     * @Author 闻柏智
     * @Date 0:06 2023/10/30
     * @Param updateUsernameVO
     * @return com.baizhi.shane.common.base.Result<java.lang.String>
     **/
    @ApiOperation(value="用户修改密码", notes="用户修改密码")
    @PostMapping(value = ApiMapping.UPDATE_PASSWORD)
    public Result<SuccessedOutVO> updatePassword(@RequestBody UpdatePasswordVO updateUsernameVO) {
        SuccessedOutVO successedOutVO = sysUserService.updatePassword(updateUsernameVO);
        return Result.OK(successedOutVO);
    }

    /**
     * 验证传入的字符串是否是用户的真实密码
     * 为真返回1，为假返回0
     *
     * @Author 闻柏智
     * @Date 23:51 2023/12/28
     * @Param checkUserPasswordInVO
     * @return com.baizhi.shane.common.base.Result<java.lang.String>
     **/
    @ApiOperation(value="验证传入的字符串是否是用户的真实密码", notes="验证传入的字符串是否是用户的真实密码")
    @PostMapping(value = ApiMapping.CHECK_USER_PASSWORD)
    public Result<String> checkUserPassword(@RequestBody CheckUserPasswordInVO checkUserPasswordInVO) {
        String passwordIsTrue =  this.sysUserService.checkUserPassword(checkUserPasswordInVO);
        return Result.OK(passwordIsTrue);
    }

    /**
     * 修改用户信息
     *
     * @Author 闻柏智
     * @Date 18:35 2023/12/29
     * @Param sysUser
     * @return com.baizhi.shane.common.base.Result<com.baizhi.shane.sys.vo.SuccessedOutVO>
     **/
    @ApiOperation(value="修改用户信息", notes="修改用户信息")
    @PostMapping(value = ApiMapping.UPDATE_USER_INFO)
    public Result<SuccessedOutVO> updateUserInfo(@RequestBody SysUser sysUser) {
        SuccessedOutVO successedOutVO = sysUserService.updateUserInfo(sysUser);
        return  Result.OK(successedOutVO);
    }

    @ApiOperation(value="判断当前登录用户是否具有管理员权限", notes="判断当前登录用户是否具有管理员权限")
    @GetMapping(value = ApiMapping.CHECK_NOW_LOGIN_USER_HAS_ADMIN_ROLE)
    public Result<SuccessedOutVO> checkNowLoginUserHasAdminRole() {
        SuccessedOutVO successedOutVO = this.sysUserService.checkNowLoginUserHasAdminRole();
        return Result.OK(successedOutVO);
    }

    @ApiOperation(value="跨域测试", notes="跨域测试")
    @GetMapping(value = "/hi")
    public String hi() {
//        LoginUserVO loginUserVO = sysUserService.getLoginUserInfo();
        return "你好";
    }

}
